package com.ktwlsoft.archivemanage.domain.business.businessService;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.AccountRepositoryService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.DepartmentRepositoryService;
import com.ktwlsoft.archivemanage.domain.dao.entity.Account;
import com.ktwlsoft.archivemanage.domain.dao.entity.Department;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import com.ktwlsoft.archivemanage.payload.department.DepartmentManageSaveRequest;
import com.ktwlsoft.archivemanage.payload.department.DepartmentManageUpdateRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 部门业务
 *
 * @author zhuhe on 2019/10/28 9:29
 * @version 1.0
 */
@Component
public class DepartmentManageService {

    /**部门业务*/
    private final DepartmentRepositoryService departmentRepositoryService;

    /**账号*/
    private final AccountRepositoryService accountRepositoryService;

    public DepartmentManageService(DepartmentRepositoryService departmentRepositoryService, AccountRepositoryService accountRepositoryService) {
        this.departmentRepositoryService = departmentRepositoryService;
        this.accountRepositoryService = accountRepositoryService;
    }

    /**
      * 部门管理列表 on 2019/10/28 9:35
      *
      * @param state state
      * @return Result<?>
      */
    public Result<?> findPage(int pn,int pageSize,Department.State state){
        Department department = new Department();
        department.setId(null);
        department.setCreatedAt(null);
        department.setCreator(null);
        department.setCreatorId(null);
        department.setUpdateAt(null);
        if(state != null && !"".equals(state.toString().trim())){
            department.setState(state);
        }
        PageInfo<Department> pageInfo = departmentRepositoryService.findPage(pn,pageSize,department);
        return Result.ok(pageInfo);
    }

    /**
      * 部门管理新增 on 2019/10/28 9:44
      *
      * @param request request
      * @return Result<?>
      */
    public Result<?> save(DepartmentManageSaveRequest request){
        Department department = new Department();
        BeanUtils.copyProperties(request,department);
        department.setState(Department.State.NORMAL);
        try {
            departmentRepositoryService.insert(department);
        }catch (Exception e){
            e.printStackTrace();
            return Result.body(null, ResultType.SAVING_ERROR);
        }
        return Result.ok(department.getId());
    }

    /**
     * 部门管理修改 on 2019/10/28 9:44
     *
     * @param request request
     * @return Result<?>
     */
    public Result<?> modify(DepartmentManageUpdateRequest request){
        if(request.getId() == null || request.getId() <= 0){
            return Result.body(null, ResultType.CHECK_ERROR).withMessage("id不能为空");
        }
        Department department = new Department();
        BeanUtils.copyProperties(request,department);
        List<Account> accounts = accountRepositoryService.getByDepartmentId(department.getId());
        List<Long> list = Lists.newArrayList();
        if(accounts != null && accounts.size() > 0){
            accounts.forEach(value ->{
                list.add(value.getId());
            });
        }
        try {
            departmentRepositoryService.update(department);
            if(list.size() > 0){
                JSONObject object = new JSONObject();
                object.put("departmentName",department.getDepartment());
                object.put("list",list);
                accountRepositoryService.manyUpdate(object);
            }
        }catch (Exception e){
            e.printStackTrace();
            return Result.body(null, ResultType.UPDATE_ERROR);
        }
        return Result.ok(department.getId());
    }

    /**
      * 部门管理删除 on 2019/10/28 10:03
      *
      * @param id id
      * @return Result<?>
      */
    public Result<?> deleteById(Long id){
        List<Account> accounts = accountRepositoryService.getByDepartmentId(id);
        if(accounts != null && accounts.size() > 0){
            return Result.body(id,ResultType.CHECK_ERROR).withMessage("数据已被引用,无法删除");
        }
        Department department = departmentRepositoryService.getById(id);
        if(department == null){
            return Result.body(id,ResultType.CHECK_ERROR).withMessage("该数据不存在");
        }
        try {
            departmentRepositoryService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            return Result.body(id,ResultType.DELETE_ERROR);
        }
        return Result.ok(department.getDepartment());
    }
}